"""Q&A API我们定义了对."""
from typing_extensions import Annotated

from fastapi import Depends, File, Form, HTTPException, Path, Request, UploadFile

from core.models import Member
from lib.common import filter_words
from api.v1.dependencies.member import get_current_member
from api.v1.service.qa import QaFileServiceAPI, QaServiceAPI
from api.v1.models.qa import QaContent, QaContentData


def get_qa_content(
    service: Annotated[QaServiceAPI, Depends()],
    member: Annotated[Member, Depends(get_current_member)],
    qa_id: Annotated[int, Path(..., title="Q&A Idie", description="Q&A Idie")],
):
    """Q&A 我们查找信息."""
    return service.read_qa_content(member, qa_id)


def get_qa_file(
    file_service: Annotated[QaFileServiceAPI, Depends()],
    qa: Annotated[QaContent, Depends(get_qa_content)],
    file_index: Annotated[int, Path(..., title="Q&A 文件编号", description="向上看 Q&A 文件编号")]
) -> dict:
    """Q&A 我查了一下文件."""
    return file_service.get_file(qa, file_index)


def validate_data(
    request: Request,
    data: QaContentData,
):
    """Q&A 注册/在更正时验证信息."""
    subject_filter_word = filter_words(request, data.qa_subject)
    content_filter_word = filter_words(request, data.qa_content)
    if subject_filter_word or content_filter_word:
        word = subject_filter_word if subject_filter_word else content_filter_word
        raise HTTPException(400, f"标题/内容上禁止使用的单词({word})包括.")

    return data


def get_upload_file_data(
    file_service: Annotated[QaFileServiceAPI, Depends()],
    file1: Annotated[UploadFile, File(title="附件1")] = None,
    file2: Annotated[UploadFile, File(title="附件2")] = None,
    file_del1: Annotated[int, Form(title="附件1 是否删除")] = 0,
    file_del2: Annotated[int, Form(title="附件2 是否删除")] = 0,
) -> dict:
    """Q&A 验证上传文件."""
    if file1:
        file_service.validate_upload_file(file1)
    if file2:
        file_service.validate_upload_file(file2)

    return {
        "file1": file1,
        "file2": file2,
        "file_del1": file_del1,
        "file_del2": file_del2,
    }
